#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if(!l1)
        return l2;
    if(!l2)
        return l1;

    ListNode *pMain = (l1->val < l2->val) ? (l1) : (l2);
    ListNode *p1 = pMain->next;
    ListNode *p2 = (l1->val < l2->val) ? (l2) : (l1);
    ListNode *pWrite = pMain;

    while(p1 && p2){
        if(p1->val > p2->val){
            pWrite->next = p2;
            p2 = p2->next;
        }
        else{
            pWrite->next = p1;
            p1 = p1->next;
        }
        pWrite = pWrite->next;
    }

    ListNode *pRead = (p1 == NULL ? p2 : p1);
    pWrite->next = pRead;

    return pMain;
}


int main(){

    return 0;
}